package com.peace.hospital.service.impl;


import com.peace.hospital.entity.common.Druginformation;
import com.peace.hospital.entity.drugstorage.*;
import com.peace.hospital.mapper.drugstorage.*;
import com.peace.hospital.mapper.publicn.DruginformationMapper;
import com.peace.hospital.service.DrugStoProService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DrugStoProServiceimpl implements DrugStoProService {

    @Autowired
    private DruginformationMapper druginformationMapper;
    @Autowired
    private WaentryMapper waentryMapper;
    @Autowired
    private DruginformationandwaentryMapper druginformationandwaentryMapper;
    @Autowired
    private DamagedrugMapper damagedrugMapper;
    @Autowired
    private FrmlossMapper frmlossMapper;
    @Autowired
    private OutwarehouseMapper outwarehouseMapper;
    @Autowired
    private OutdrugMapper outdrugMapper;

//    查询药品信息
    public Druginformation findDrugInfo(Druginformation din) {
        Druginformation din1= druginformationMapper.findDrug(din);
        return din1;
    }

//    更新药品信息
    public int updateDrug(Druginformation din){
        return druginformationMapper.updateDrug(din);
    }

    //添加入库日期和入库人员
    @Override
    public int addWaentry(Waentry waentry) {
        int i=waentryMapper.addWaentry(waentry);
        return i;
    }
    //    添加druginformationandwaentry对象的信息
    @Override
    public int addDruAndWa(int drugid, int waeid) {
        System.out.println(drugid+">>>>>>>"+waeid+">>>>>>>>>");
        int i= druginformationandwaentryMapper.addDruAndWa(drugid,waeid);
        return i;
    }

    @Override
    public int findLastId() {
        return waentryMapper.findLastId();
    }

    @Override
    public int findDruginLastId() {
        return druginformationMapper.findDruginLastId();
    }
// 查询入库单信息
    @Override
    public Waentry findWaeById(int waeid) {
        Waentry waentry=waentryMapper.findById(waeid);
        return waentry;
    }
    //    根据入库日期和员工id查询入库信息
    @Override
    public List<Waentry> findWaeByDateAndStaid(Waentry waentry) {
        System.out.println(waentry.getWaedate());
        List<Waentry> waetryList= waentryMapper.findByWaedAndStaId(waentry);
        for(Waentry waentry1:waetryList){
            System.out.println(waentry1);

        }
        return waetryList;
    }
    //    根据入库id查询入库的药品
    @Override
    public List<Druginformationandwaentry> findDrugByWaeId(Waentry waentry) {
        List<Druginformationandwaentry> druginformationandwaentryList=druginformationandwaentryMapper.selectById(waentry);
        return druginformationandwaentryList;
    }
//添加报损单信息
    @Override
    public int addFromloss(Frmloss frmloss) {

        return frmlossMapper.addFromloss(frmloss);
    }
//添加报损药品信息
    @Override
    public int addDamageDrug(Damagedrug damagedrug) {
        return damagedrugMapper.addDamageDrug(damagedrug);
    }

    @Override
    public Frmloss findFrmloss() {
        return frmlossMapper.findFrmloss();
    }
    // 查询报损单信息
    @Override
    public List<Frmloss> findFrmByFrmid(Frmloss frmloss) {

        return frmlossMapper.findFrmByFrmid(frmloss);
    }
//查询报损药品
    @Override
    public List<Damagedrug> findDamageById(Damagedrug damagedrug) {
        return damagedrugMapper.findDamageById(damagedrug);
    }
//添加入库单
    @Override
    public int addOutwa(Outwarehouse outwarehouse) {
        int i =outwarehouseMapper.addOutwa(outwarehouse);
        return i;
    }
    //添加出库药品信息
    public int addOutDrug(Outdrug outdrug){
        int i=outdrugMapper.addOutDrug(outdrug);
        return i;
    }
    //查询入库最后一条入库单id

    public Outwarehouse findLastOutwa(){
        Outwarehouse outwarehouse=outwarehouseMapper.findLastOutwa();
        return outwarehouse;
    }
    //    出库时删除入库药品信息
    @Override
    public int updateDruginfo(Outdrug outdrug) {
        System.out.println(outdrug);
        int i=druginformationMapper.updateDruginfo(outdrug);
        return i;
    }
    //    查询出库库单信息
    @Override
    public List<Outwarehouse> findOutwa(Outwarehouse outwarehouse) {
        return outwarehouseMapper.findOutwa(outwarehouse);
    }
    //    出库单id查询出库药品
    public List<Outdrug> findOutDByOutid(Outdrug outdrug){
        return outdrugMapper.findOutDByOutid(outdrug);
    }

    @Transactional
    @Override
    public String addOutWInfo(DruginfoAndAnyDto druginfoAndAnyDto, Outwarehouse outwarehouse) {
//        System.out.println(outwarehouse);
        int i=this.addOutwa(outwarehouse);
        if(true){
            throw new RuntimeException();
        }

        if(i==0){
            return "fail";
        }else{
            Outwarehouse outwarehouse1=this.findLastOutwa();
            for(Outdrug outdrug:druginfoAndAnyDto.getOutdrug()){

                outdrug.setOutid(outwarehouse1.getOutid());
                int j=this.addOutDrug(outdrug);
                if(j==0){
                    return "fail";
                }else{
                    int m=this.updateDruginfo(outdrug);
                    if(m==0){
                        return "fail";
                    }
                }
            }
            return "success";
        }
    }
}
